package mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-04-05 17:31
 */
public class MongoRunnable implements Runnable{

    private int num;

    private int skip;

    private int limit;

    private static MongoCollection<Document> collection = MongoTest.getCollection("TEST_2017-04-05");

    public MongoRunnable(int skip, int limit) {
        this.skip = skip;
        this.limit = limit;
    }

    @Override
    public void run() {
        long s = System.currentTimeMillis();
        String timeS = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
        DBObject obj = new BasicDBObject();
        DBObject timeObj = new BasicDBObject("$regex", timeS); // 错误日期
        timeObj.put("$options", "i");
        obj.put("time", timeObj);
        DBCursor c = SpringMongo.mongoTemplate2.getCollection("api_log").find(obj).skip(skip).limit(limit);
        num = c.size();
        List<DBObject> list = c.toArray();
        System.out.println(Thread.currentThread() + " : " + (System.currentTimeMillis() - s) + " : " + (skip + limit) + " : " + num);
        List<Document> l = new ArrayList<Document>();
        for (DBObject dbObject : list) {
            Document d = Document.parse(dbObject.toString());
            d.remove("_id");
            l.add(d);
        }
        collection.insertMany(l);
    }
}
